//
//  QYXMonthRankingViewController.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXMonthRankingViewController.h"
#import "QYXTopRankingModel.h"
#import "QYXTopRankingTableViewCell.h"
#import "QYXRankingInformationViewController.h"//详情页控制器
#import "AppDelegate.h"

@interface QYXMonthRankingViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 月排行表视图 */
@property (nonatomic, strong) UITableView *monthRankingTableView;

/** 月排行数据源数组 */
@property (nonatomic, strong) NSMutableArray *monthRankingDataSource;

@end

@implementation QYXMonthRankingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatMonthRankingView];
    [self getMonthRankingData];
 
}



- (void)creatMonthRankingView

{
    
    //创建表视图:
    
    self.monthRankingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49 - 20) style:UITableViewStylePlain];
    self.monthRankingTableView.delegate = self;
    self.monthRankingTableView.dataSource = self;
    self.monthRankingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_monthRankingTableView];
    //注册:
    
    [self.monthRankingTableView registerClass:[QYXTopRankingTableViewCell class] forCellReuseIdentifier:@"monthRankingCell"];
    
    
}



- (void)getMonthRankingData

{
    NSString *urlString = @"http://baobab.wandoujia.com/api/v3/ranklist?_s=e38804f2bff56ec50e8c7246f6a6fabb&f=iphone&net=wifi&strategy=monthly&u=aafd829df8ff2d571a778ed3cca27eb68b16bd7d&v=2.2.0&vc=721";
    
    //自动缓存策略:
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    
    [QYXNetTool getNetWithUrl:urlString urlBody:nil header:nil response:QYXJSON success:^(id result) {
        
        //开始解析数据:
        self.monthRankingDataSource = [NSMutableArray array];
        
        NSMutableDictionary *qyxDic = (NSMutableDictionary *)result;
        
        NSArray *array = [NSArray arrayWithArray:[qyxDic objectForKey:@"itemList"]];
        
        for (NSDictionary *dic in array) {
            
            NSMutableDictionary *tempDic = [dic objectForKey:@"data"];
            
            QYXTopRankingModel *topRankingModel = [[QYXTopRankingModel alloc] initWithDic:tempDic];
            
            [self.monthRankingDataSource addObject:topRankingModel];
            
        }
        
        [self.monthRankingTableView reloadData];
        
        
    } failure:^(NSError *error) {
        NSLog(@"请求失败 : %@", error);
    }];
    
    
    
}

#pragma mark -- 签协议必须实现协议方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.monthRankingDataSource.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    QYXTopRankingModel *model = [self.monthRankingDataSource objectAtIndex:indexPath.row];
    
    QYXTopRankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"monthRankingCell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.topRankingModel = model;
    
    cell.rankingNumber = indexPath.row + 1;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return kScreenHeight / 3;
    
    
}



#pragma mark -- 点击跳转:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5f;
    transition.delegate = self;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    transition.subtype = kCATransitionFromTop;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    QYXRankingInformationViewController *rankingInforVC = [[QYXRankingInformationViewController alloc] init];
    rankingInforVC.RankingSelectNum = indexPath.row;
    rankingInforVC.rankingInforDataSource = self.monthRankingDataSource;
    AppDelegate *tempAppDe = [UIApplication sharedApplication].delegate;
    tempAppDe.tabBar.backView.hidden = YES;
    [self.navigationController pushViewController:rankingInforVC animated:NO];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
